/*
 * Copyright (C) 2015-2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include "PlanetWars2dRT-core/specific/controller/universe_update.h"
#include "PlanetWars2dRT-core/specific/controller/universe_collisions.h"
#include "PlanetWars2dRT-core/specific/controller/planet.h"
#include "PlanetWars2dRT-core/specific/controller/ship.h"
#include "PlanetWars2dRT-core/specific/model/universe_ships.h"
#include "PlanetWars2dRT-core/specific/model/universe_planets.h"


EXTERN_C_BEGIN


void
universe_planets_update_once(universe_t* a_universe)
{
  const size_t nb_planets = universe_get_nb_planets(a_universe);
  for(size_t i=0; i < nb_planets; ++i)
    {
      planet_update_once(universe_get_planet_no_check(a_universe, i),
			 a_universe);
    }
}

void
universe_ships_update_once(universe_t* a_universe)
{
  const size_t nb_ships = universe_get_nb_ships(a_universe);
  for(size_t i=0; i < nb_ships; ++i)
    {
      ship_update_once(universe_get_ship_no_check(a_universe, i));
    }
}

void
universe_update_once(universe_t* a_universe)
{
  universe_planets_update_once(a_universe);
  universe_ships_update_once(a_universe);
  
  universe_do_collisions_of_ships_with_ships(a_universe);
  universe_do_collisions_of_ships_with_planets(a_universe);
  
  universe_remove_destroyed_ships(a_universe);
}

void
universe_update(universe_t* a_universe, unsigned short nb_times)
{
  for(unsigned short i=0; i < nb_times; ++i)
    {
      universe_update_once(a_universe);
    }
}


EXTERN_C_END
